package cn.ranyue.org.controller;

import cn.ranyue.basic.util.AjaxResult;
import cn.ranyue.basic.util.QueryResult;
import cn.ranyue.org.domain.Employee;
import cn.ranyue.org.query.EmployeeQuery;
import cn.ranyue.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "用户接口",description = "部门接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
   /* @PostMapping
    @DeleteMapping
    @PutMapping
    @PathVariable:从url中取值
    此时的请求方式：get请求 http://locallhost:8080/dept/1
    */

   @GetMapping("/{id}")
   @ApiOperation(value = "通过id查询某一条数据")
    public Employee queryById(@PathVariable  Long id){
        return employeeService.loadOne(id);

    }
    /*
    * 查询所有
    * 此时的请求方式：get请求 http://locallhost:8080/dept
    * */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Employee> queryAll(){
       return employeeService.loadAll();
    }
    /*分页查询
    * @RequestBody：从请求体获取jsonge
    * 此时的请求方式：post请求 http://locallhost:8080/dept
    * */
  /*  @PostMapping
    @ApiOperation(value = "分页查询")
    public QueryResult<Employee> queryPage(@RequestBody EmployeeQuery query){
       return employeeService.queryPage(query);

    }*/
    /*
    * 删除一条数据
    * 此时的请求方式：delete请求 http://locallhost:8080/dept/id
    * */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过删除某一条数据")
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
           e.printStackTrace();
           return AjaxResult.me().setSuccess(false).setMassage("操作失败");
        }
    }
    /*
    * 添加和修改数据
    * 此时的请求方式：put请求 http://locallhost:8080/dept/id
    * 又id为修改数据 没有id为添加数据
    * */
    @PutMapping
    @ApiOperation(value = "添加或者修改一条数据")
    public AjaxResult addAndupdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.add(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("操作失败");
        }
    }
    /*批量删除*/
    //@PatchMapping
   /* public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("操作失败");
        }
    }*/
}
